﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SEO.Entity;

namespace SEO.Business
{
    /// <summary>
    /// 关键字优化量统计
    /// </summary>
    public class BisKeywordsCount
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetList(string strName, int nAdminID, int nSortID, int nSysLock, int nDiyLock, int nBeginNewsCount, int nEndNewsCount, int nBeginLinkCount, int nEndLinkCount, DateTime dBeginTime, DateTime dEndTime, int nPageIndex, int nPageSize, ref int nPageCount, ref int nRecordCount)
        {
            SqlParameter[] parms = new SqlParameter[15];
            parms[0] = new SqlParameter("@Name", strName);
            parms[1] = new SqlParameter("@AdminID", nAdminID);
            parms[2] = new SqlParameter("@SortID", nSortID);
            parms[3] = new SqlParameter("@SysLock", nSysLock);
            parms[4] = new SqlParameter("@DiyLock", nDiyLock);
            parms[5] = new SqlParameter("@BeginNewsCount", nBeginNewsCount);
            parms[6] = new SqlParameter("@EndNewsCount", nEndNewsCount);
            parms[7] = new SqlParameter("@BeginLinkCount", nBeginLinkCount);
            parms[8] = new SqlParameter("@EndLinkCount", nEndLinkCount);
            parms[9] = new SqlParameter("@BeginTime", dBeginTime);
            parms[10] = new SqlParameter("@EndTime", dEndTime);
            parms[11] = new SqlParameter("@PageIndex", nPageIndex);
            parms[12] = new SqlParameter("@PageSize", nPageSize);

            //返回值
            parms[13] = new SqlParameter("@PageCount", nPageCount);
            parms[13].Direction = ParameterDirection.Output;
            parms[14] = new SqlParameter("@RecordCount", nRecordCount);
            parms[14].Direction = ParameterDirection.Output;

            DataTable dt = SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.StoredProcedure,
                "GetKeywordsCountList",
                parms).Tables[0];
            nPageCount = Convert.ToInt32(parms[13].Value);
            nRecordCount = Convert.ToInt32(parms[14].Value);
            return dt;
        }


        /// <summary>
        /// 删除报表
        /// </summary>
        /// <param name="dDayTime"></param>
        /// <returns></returns>
        public bool Delete(DateTime dDayTime)
        {
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, "delete from [KeywordsCount] where [CreateTime]=@DayTime", new SqlParameter("@DayTime", dDayTime)) > 0;
        }

        /// <summary>
        /// 统计数量
        /// </summary>
        /// <param name="nAdminID"></param>
        /// <param name="nKeywordsID"></param>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <returns></returns>
        public int KeywordsCountByAdmin(int nAdminID, int nKeywordsID, int nUrlType, int nIsPass, DateTime dBeginTime, DateTime dEndTime)
        {
            string strSql = "select count([ID]) from [ArticleSEO] where [AdminID]=@AdminID and [KeywordsID]=@KeywordsID and [UrlType]=@UrlType and (@IsPass=-1 or [IsPass]=@IsPass) and [CreateTime] between @BeginTime and @EndTime";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@AdminID", nAdminID));
            parms.Add(new SqlParameter("@KeywordsID", nKeywordsID));
            parms.Add(new SqlParameter("@UrlType", nUrlType));
            parms.Add(new SqlParameter("@IsPass", nIsPass));
            parms.Add(new SqlParameter("@BeginTime", dBeginTime));
            parms.Add(new SqlParameter("@EndTime", dEndTime));
            object retVal = SqlHelper.ExecuteScalar(SqlHelper.database, CommandType.Text, strSql, parms.ToArray());
            return retVal == null ? 0 : Convert.ToInt32(retVal);
        }

        /// <summary>
        /// 统计
        /// </summary>
        /// <param name="nIsPass"></param>
        /// <param name="dBeginTime"></param>
        /// <param name="dEndTime"></param>
        /// <returns></returns>
        public DataTable GetKeywordsCount(int nIsPass, DateTime dBeginTime, DateTime dEndTime)
        {
            string strSql = "select [KID],[AdminID],[UrlType],count([ID]) [AllCount],convert(char(10),[CreateTime],23) as [day] from [v_KeywordsArticle] where (@IsPass=-1 or [IsPass]=@IsPass) and [CreateTime] between @BeginTime and @EndTime group by [KID],[AdminID],[UrlType],convert(char(10),[CreateTime],23)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@IsPass", nIsPass));
            parms.Add(new SqlParameter("@BeginTime", dBeginTime));
            parms.Add(new SqlParameter("@EndTime", dEndTime));
            return SqlHelper.ExecuteDataset(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()).Tables[0];
        }

        /// <summary>
        /// 新增关键字优化量统计
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool Insert(KeywordsCount Info)
        {
            string strSql = "insert into [KeywordsCount]([KeywordsID],[AdminID],[AllNewsCount],[NewsCount],[AllLinkCount],[LinkCount],[CreateTime]) values(@KeywordsID,@AdminID,@AllNewsCount,@NewsCount,@AllLinkCount,@LinkCount,@CreateTime)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@KeywordsID", Info.KeywordsID));
            parms.Add(new SqlParameter("@AdminID", Info.AdminID));
            parms.Add(new SqlParameter("@AllNewsCount", Info.AllNewsCount));
            parms.Add(new SqlParameter("@NewsCount", Info.NewsCount));
            parms.Add(new SqlParameter("@AllLinkCount", Info.AllLinkCount));
            parms.Add(new SqlParameter("@LinkCount", Info.LinkCount));
            parms.Add(new SqlParameter("@CreateTime", Info.CreateTime));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }
    }
}
